<?php

namespace app\admin\server\order;

use app\admin\model\order\OrderEntityModel;
use app\admin\model\user\UserModel;
use app\admin\ServerBase;
use app\api\model\order\OrderMusicDetailApiModel;
use think\Request;

class OrderEntityServer extends ServerBase
{

    protected $model;
    protected $request;
    /**
     * @var mixed
     */
    protected $param;

    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new OrderEntityModel();
    }
    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function getDetail(){
        $id =  $this->request->param('id');
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        $order_detail = OrderMusicDetailApiModel::getByOrderId($id);
        $buyer = UserModel::getInfoById($info['buyer_id']);
        $this->getMessageCode('adminMessage.success.select',2);
        return ['info'=>$info,'detail'=>$order_detail,'buyer'=>$buyer];
    }

    function delivery(){
        $id = $this->param['id']??0;
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        if(!in_array($info['status'],[20,30])){
            $this->ERROR='该订单不能发货';
            return false;
        }
        $express_com = $this->param['express_com'];
        $express_no = $this->param['express_no'];
        if(empty($express_com) || empty($express_no)){
            $this->ERROR='请选择快递公司和快递单号';
            return false;
        }
        $info->status = 30;
        $info->express_com = $express_com;
        $info->express_no = $express_no;
        if(empty($info['delivery_time'])){
            $info->delivery_time = date('Y-m-d H:i:s');
        }
        if($info->save()){
            return true;
        }else{
            $this->ERROR='发货失败';
            return false;
        }
    }
    function complete(){
        $id = $this->param['id']??0;
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        if(!in_array($info['status'],[20])){
            $this->ERROR='该订单不能完成';
            return false;
        }
        $info->status = 30;
        if(empty($info['complete_time'])){
            $info->delivery_time = date('Y-m-d H:i:s');
        }
        if($info->save()){
            return true;
        }else{
            $this->ERROR='完成失败';
            return false;
        }
    }
    function take(){
        $id = $this->param['id']??0;
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        if($info['status'] != 30){
            $this->ERROR='该订单不能收货';
            return false;
        }
        $info->status = 40;
        $info->take_time = date('Y-m-d H:i:s');
        if($info->save()){
            return true;
        }else{
            $this->ERROR='收货失败';
            return false;
        }
    }
}